package com.zhs.gccs.view;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.zhs.gccs.R;
import com.zhs.gccs.adapter.WorktableMenuAdapter;
import com.zhs.gccs.model.WorkTableMenu;
import com.zhs.gccs.recycleview.GridItemDecoration;
import com.zhs.gccs.utils.ScreenUtils;

import java.util.ArrayList;
import java.util.List;

public class WorkMenuView extends BaseView{
    private TextView tv_title;
    private RecyclerView recycler_view;//
    private WorktableMenuAdapter menuAdapter;
    private List<WorkTableMenu> menus;


    public WorkMenuView(@NonNull Context context) {
        super(context);
    }

    public WorkMenuView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public WorkMenuView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View view = LayoutInflater.from(context).
                inflate(R.layout.view_work_menu, this, true);
        tv_title  = view.findViewById(R.id.tv_title);
        recycler_view = view.findViewById(R.id.recycler_view);
        recycler_view.setLayoutManager(new StaggeredGridLayoutManager(2,
                StaggeredGridLayoutManager.VERTICAL));
        recycler_view.addItemDecoration(new GridItemDecoration(
                new GridItemDecoration.Builder(context)
                        .color(R.color.bg_grey)
                        .size((int) ScreenUtils.dpToPx(context,10f))));
        menus = new ArrayList<>();
        menuAdapter = new WorktableMenuAdapter(context, menus);
        recycler_view.setAdapter(menuAdapter);
        menuAdapter.setOperationListener(new WorktableMenuAdapter.OperationListener() {
            @Override
            public void onItemClick(int position) {
                WorkTableMenu menu = menus.get(position);
                Intent intent = new Intent(context,menu.getActivityClass());
                context.startActivity(intent);
            }
        });
    }

    public WorkMenuView setTitle(String title){
        tv_title.setText(title);
        return this;
    }
    public WorkMenuView setMenus(List<WorkTableMenu> menus){
        this.menus.addAll(menus);
        menuAdapter.notifyDataSetChanged();
        return this;
    }
}
